package com.vancouver.knows.business.tracker;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.HomeLiveFeedInfo;
import com.vancouver.knows.business.cache.LiveFeedCache;;
import com.vancouver.knows.business.service.impl.LiveFeedHandler;
import com.vancouver.knows.business.task.mark.LiveFeedResultTaskMark;

import java.util.HashMap;
import java.util.List;

/**
 * Created by llf on 2016/6/16.
 */
public class LiveFeedTracker  extends ABInvokeTracker {
    public LiveFeedTracker(BusinessModule module, IResultReceiver resultReceiver) {
        super(module, resultReceiver);
    }

    @Override
    protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
        List<HomeLiveFeedInfo> liveFeedListInfo = module.getCacheManager().getHomeLiveFeedInfoCache();
        liveFeedListInfo.clear();
        LiveFeedCache liveFeedCache =  module.getCacheManager().getLiveFeedCache();
        liveFeedCache.deleteItemInfoIndexFromCache(taskMark);
    }

    @Override
    public String TAG() {
        return  LiveFeedTracker.class.getSimpleName();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleResult(OperateResult result) {
        PageableResult pageableResult = (PageableResult)result.getResultData();
        if(pageableResult == null) return;

        HashMap<String, Object> map = (HashMap<String, Object>) pageableResult.getContent();
        LiveFeedResultTaskMark liveFeedTaskMark = (LiveFeedResultTaskMark) result.getTaskMark();
        liveFeedTaskMark.setPageInfo(pageableResult.getPageInfo());
        List<HomeLiveFeedInfo> liveFeedInfoList = (List<HomeLiveFeedInfo>) map.get(LiveFeedHandler.HOMEPAGE_LIVE_FEED);
        module.getCacheManager().getHomeLiveFeedInfoCache().addAll(liveFeedInfoList);
        module.getCacheManager().getLiveFeedCache().addItemInfoToCache(liveFeedTaskMark, liveFeedInfoList);
    }
}
